﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ASoft.Base;
using Asoft.Web.Models;
using Newtonsoft.Json;

namespace Asoft.Web.Controllers
{
    [LoginCheck]
    [ExceptionFilter]
    public abstract class EntityController<TRepository, TEntity> : Controller
        where TRepository : IRepository<TEntity>
        where TEntity : class
    {
        protected int PageIndex
        {
            get
            {
                var str = Request["pageindex"];
                int result = 1;
                if (string.IsNullOrEmpty(str))
                {
                    return result;
                }
                if (int.TryParse(str, out result))
                {
                    return result;
                }
                return result;
            }
        }

        protected int PageSize
        {
            get { return 10; }
        }

        public ActionResult Index1()
        {
            var condition = new Dictionary<string, string>();
            var result = GetIndex1PageResult();
            ViewData["grid"] = result.Datas;
            ViewData["datacount"] = result.DataCount;
            ViewData["pagecount"] = result.PageCount;
            ViewData["pageindex"] = result.PageIndex;
            ViewData["pagesize"] = result.PageSize;
            BeforeIndex1ReturnView();
            return View();
        }

        protected virtual void BeforeIndex1ReturnView()
        {
        }

        protected virtual PageResult<TEntity> GetIndex1PageResult()
        {
            return Repository.GetPageResult(null, PageIndex, PageSize, null, null);
        }

        public virtual ActionResult Index()
        {
            return View();
        }

        TRepository _Repository;
        protected TRepository Repository
        {
            get
            {
                if (_Repository == null)
                {
                    _Repository = Activator.CreateInstance<TRepository>();
                }
                return _Repository;
            }
        }

        public ActionResult JQGridDatas()
        {
            Dictionary<string, string> condition = new Dictionary<string, string>();

            OnBeforeJQGridDatasGetCondition(condition);

            var pageResult = Repository.GetPageResult(
                condition,
                this.JQGridPage(),
                this.JQGridRows(),
                new string[] { this.JQGridSidx() },
                new string[] { this.JQGridSord() });
            var gridResult = new GridResult();
            gridResult.AddRange(pageResult.Datas);
            gridResult.DataCount = pageResult.DataCount;
            gridResult.PageIndex = pageResult.PageIndex;
            gridResult.PageCount = pageResult.PageCount;
            gridResult.PageSize = pageResult.PageSize;
            var result = JsonConvert.SerializeObject(gridResult);
            return Content(result);
        }

        protected virtual void OnBeforeJQGridDatasGetCondition(Dictionary<string, string> condition)
        {

        }

        public ActionResult JQGridEdit()
        {
            var entity = Activator.CreateInstance<TEntity>();
            var oper = this.JQGridOper();
            if (oper != EOper.Del)
            {
                ProcessJQGridEdit(entity);
            }

            var id = this.JQGridEditId();
            if (oper == EOper.Add)
            {
                Repository.Insert(entity);
            }
            else if (oper == EOper.Edit)
            {
                ((dynamic)entity).Id = id;
                Repository.Update(entity);
            }
            else
            {
                Repository.Delete(id);
            }
            return Content("Ok");
        }

        protected abstract void ProcessJQGridEdit(TEntity entity);

        protected JsonResult GetJsonResult(string status, string msg)
        {
            return Json(new { status = status, msg = msg });
        }

    }
}